package com.test;

import android.Manifest;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements  View.OnClickListener {

    private Button button1;
    private Button button2;
    private Button button3;
    private DownloadService.DownloaderBinder downloaderBinder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button1=(Button)findViewById(R.id.button1);
        button2=(Button)findViewById(R.id.button2);
        button3=(Button)findViewById(R.id.button3);
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);

        Intent intent = new Intent(this,DownloadService.class);
        startService(intent);
        bindService(intent,connection,BIND_AUTO_CREATE);

        if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){

            ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
        }//what will com if no pemmission;




    }

    @Override
    public void onClick(View v) {
        if(downloaderBinder == null){

            return ;
        }

        switch (v.getId()){

            case R.id.button1:

                Logs.i(good());

                String url = "http://192.168.201.2/upload/gold4.gz";
                downloaderBinder.startDownload(url);


                break;

            case R.id.button2:
                downloaderBinder.pauseDownload();
                break;

            case R.id.button3:

                downloaderBinder.cancelDownload();

                break;



        }

    }


    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {

                downloaderBinder = (DownloadService.DownloaderBinder)service;



        }

        @Override
        public void onServiceDisconnected(ComponentName name) {


        }
    };

    @Override
    public void onRequestPermissionsResult(int requestCode,String[] permissions, int[] grantResults) {


       switch (requestCode){

           case 1:
               if(grantResults.length >0 && grantResults[0] != PackageManager.PERMISSION_GRANTED){

                   Toast.makeText(this,"拒绝权限将无法使用程序",Toast.LENGTH_LONG).show();
                  // finish();
               }
           break;

           default:

       }
    }


    @Override
    protected void onDestroy() {


        super.onDestroy();
        unbindService(connection);
    }
    public native String good();
    static{

        System.loadLibrary("test");
    }
}
